package com.gzhryc.shared_device.oem.code.statistics.dao;

import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.DateBranchTableDao;
import com.gzhryc.jdbc.Query;
import com.gzhryc.shared_device.oem.code.statistics.dao.db.PlaceUserProfitDayStatistics;
import com.gzhryc.shared_device.oem.code.statistics.dao.db.UserProfitDayStatistics;
import com.gzhryc.shared_device.oem.code.statistics.dao.models.ProjectSumMoneyData;
import com.gzhryc.shared_device.oem.code.statistics.dao.models.UserProfitSumMoneyData;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class UserProfitDayStatisticsDao extends DateBranchTableDao<UserProfitDayStatistics> {

    public UserProfitDayStatisticsDao(String jdbcKey) {
        super(jdbcKey, new Date());
    }

    public UserProfitDayStatisticsDao(String jdbcKey, Date date) {
        super(jdbcKey, date);
    }

    public UserProfitSumMoneyData sumMoneyData(Conditions conditions) throws SQLException {
        String sql = "SELECT SUM(order_money) AS orderMoney,SUM(income_count) AS incomeCount" +
                ",SUM(income_money) AS incomeMoney,SUM(refund_count) AS refundCount" +
                ",SUM(refund_money) AS refundMoney FROM " + this.tableName;

        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),true));
        return this.jdbcHelper.get(query,UserProfitSumMoneyData.class);
    }

    public List<UserProfitSumMoneyData> sumGroupUserIdByConditions(Conditions conditions) throws SQLException {
        String sql = "SELECT user_id AS userId,SUM(order_money) AS orderMoney" +
                ",SUM(income_count) AS incomeCount,SUM(income_money) AS incomeMoney" +
                ",SUM(refund_count) AS refundCount,SUM(refund_money) AS refundMoney" +
                ",SUM(history_refund_count) AS historyRefundCount" +
                ",SUM(history_refund_money) AS historyRefundMoney" +
                " FROM " + this.tableName;

        Query query = new Query(sql);
        query.appendSql(conditions.toSql(query.getParams(),true));
        query.appendSql(" GROUP BY user_id");
        return this.jdbcHelper.find(query,UserProfitSumMoneyData.class);
    }
}
